package com.usermanage.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.usermanage.bean.Admin;
import com.usermanage.bean.User;
import com.usermanage.service.UserService;
import com.usermanage.service.impl.UserServiceImpl;
import com.usermanage.tool.Page;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	Page p=new Page();
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if (action.equals("show")) {
			show(request, response);
		}else if (action.equals("add")) {
			add(request, response);
		}else if (action.equals("edit")) {
			edit(request, response);
		}else if(action.equals("editselect")){
			queryById(request, response);
			
		}else if (action.equals("delete")) {
			delete(request, response);
		}else if (action.equals("changecount")) {
			changeCount(request, response);
		}
	}
	/**
	 * 改变每页展示数量
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void changeCount(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//接收前台设置的每页展示数量
		Integer currCount=Integer.valueOf(request.getParameter("count"));

		p.setCurrCount(currCount);
		show(request, response);
	}
	/**
	 * 删除用户
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id=request.getParameter("id");
		System.out.println("前台需要删除的用户id为"+id);
		//创建业务逻辑访问对象
		UserService us=new UserServiceImpl();
		//访问数据库查询，返回对象
		int res=0;
		res=us.delete(id);
		if (res>0) {
			response.getWriter().write("<script language='javascript' type='text/javascript'>alert('删除成功');window.location='UserServlet?action=show';</script>");
		}else {
			response.getWriter().write("<script language='javascript' type='text/javascript'>alert('删除失败');window.location='UserServlet?action=show';</script>");
		}
	}
	/**
	 * 查找用户
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void queryById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id=request.getParameter("id");
		System.out.println("前台需要修改的用户id为"+id);
		//查询用户
		//创建业务逻辑访问对象
		UserService us=new UserServiceImpl();
		//访问数据库查询，返回对象
		User u=us.queryById(id);
		System.out.println("查询到要修改的信息为："+u);
		request.setAttribute("u", u);
		request.getRequestDispatcher("edit.jsp").forward(request, response);
	}
	/**
	 * 根据id修改用户
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id=request.getParameter("id");
		
		System.out.println("前台需要修改的用户id为"+id);
		/*获取前台需要修改的数据*/
		User u=new User();
		u.setId(Integer.valueOf(id));
		u.setPhone(request.getParameter("phone"));
		u.setAddress(request.getParameter("city"));
		//创建业务逻辑访问对象
		UserService us=new UserServiceImpl();
		//访问数据库查询，返回对象
		int res=0;
		res=us.edit(u);
		if (res>0) {
			response.getWriter().write("<script language='javascript' type='text/javascript'>alert('修改成功');window.location='UserServlet?action=show';</script>");
		}else {
			response.getWriter().write("<script language='javascript' type='text/javascript'>alert('修改失败');window.location.href='edit.jsp?backurl='+window.location.href;</script>");
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		/*获取前台数据*/
		User u=new User();
		u.setName(request.getParameter("name"));
		u.setPhone(request.getParameter("phone"));
		u.setAddress(request.getParameter("city"));
		System.out.println("前台添加的数据为"+u);
		
		//创建业务逻辑访问对象
		UserService us=new UserServiceImpl();
		//访问数据库查询，返回对象
		int res=0;
		res=us.add(u);
		if (res>0) {
			response.getWriter().write("<script language='javascript' type='text/javascript'>alert('添加成功');window.location='UserServlet?action=show';</script>");
		}else {
			response.getWriter().write("<script language='javascript' type='text/javascript'>alert('添加失败');window.location.href='add.jsp?backurl='+window.location.href;</script>");
		}
	}
	/**
	 * 展示所有
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//分页查询
		//接收前台页码信息
		String curr=request.getParameter("curr");
		System.out.println("进入页面"+curr);
		String name="";
		Cookie[] ck=request.getCookies();
		if (ck!=null) {
			for (Cookie c : ck) {
				if (c.getName().equals("name")) {
					name=c.getValue();
				}

			}
		}
		//判断是否首页
		if (curr==null) {
			p.setCurrPage(1);
		} else {
			p.setCurrPage(Integer.valueOf(curr));
		}
		//创建业务逻辑访问对象
		UserService us=new UserServiceImpl();
		//通过业务逻辑访问对象返回集合
		List<User> ulist=us.show(p);
		System.out.println("发送前台信息：");
		for (User u : ulist) {
			System.out.println(u);
		}
		//将集合放置请求体中
		request.setAttribute("ulist", ulist);
		//将分页信息放置请求体中
		request.setAttribute("page", p);

		request.setAttribute("name", new URLDecoder().decode(name, "utf-8"));
		request.getRequestDispatcher("show.jsp").forward(request, response);
		System.out.println(p);
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
